package com.ls.hnks.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ls.hnks.util.Result;
import com.ls.hnks.util.ResultCodeEnum;
import com.ls.hnks.model.Training;
import com.ls.hnks.model.TrainingAttachment;
import com.ls.hnks.model.TrainingResult;
import com.ls.hnks.model.vo.TrainCountVo;
import com.ls.hnks.service.TrainingAttachmentService;
import com.ls.hnks.service.TrainingResultService;
import com.ls.hnks.service.TrainingService;
import com.ls.hnks.mapper.TrainingMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
* @author 15375
* @description 针对表【tb_training(用户培训)】的数据库操作Service实现
* @createDate 2023-01-25 21:58:27
*/
@Service
public class TrainingServiceImpl extends ServiceImpl<TrainingMapper, Training>
    implements TrainingService{

    @Autowired
    private TrainingAttachmentService trainingAttachmentService;

    @Autowired
    private TrainingResultService trainingResultService;

    @Override
    public Result getTrainingByPage(Integer pageNo, Integer size) {
        Page<Training> page = new Page<>(pageNo,size);
        QueryWrapper<Training> trainingQueryWrapper = new QueryWrapper<>();
        trainingQueryWrapper.select("id","title","content","currentTime");
        trainingQueryWrapper.orderByDesc("currentTime");
        IPage<Training> trainingPage = this.page(page, trainingQueryWrapper);
        return Result.ok(trainingPage);
    }

    @Override
    public Result getDetails(String id) {
        Training training = this.getById(id);
        if(training == null){
            return Result.error(ResultCodeEnum.TRAIN_NOT_FOUND);
        }
        List<TrainingAttachment> trainingAttachments = trainingAttachmentService.getAttachMentByTrainId(id);
        training.setAttachmentList(trainingAttachments);
        return Result.ok(training);
    }

    @Override
    public Result finishTrain(String userId,String trainId) {
        Training byId = this.getById(trainId);
        if(byId == null){
            return Result.error(ResultCodeEnum.TRAIN_NOT_FOUND);
        }

        TrainingResult result = trainingResultService.getOne(
                new LambdaQueryWrapper<TrainingResult>()
                .eq(TrainingResult::getTrainingid, trainId)
                .eq(TrainingResult::getCreateuserid, userId));
        if(result != null){
            return Result.fail().message("已经学习过了~");
        }
        TrainingResult trainingResult = new TrainingResult();
        trainingResult.setTrainingid(Integer.parseInt(trainId));
        trainingResult.setCreateuserid(Integer.parseInt(userId));
        trainingResultService.save(trainingResult);
        return Result.ok();
    }

    @Override
    public Result countTrain(String userId) {
        int total = this.count();
        QueryWrapper<TrainingResult> resultQueryWrapper = new QueryWrapper<>();
        resultQueryWrapper.eq("CreateUserID",userId);
        int finishCount = trainingResultService.count(resultQueryWrapper);

        List<TrainCountVo> list = new ArrayList<>();
        TrainCountVo trainCountVo_1 = new TrainCountVo();
        trainCountVo_1.setName("未完成");
        trainCountVo_1.setValue(total - finishCount);
        list.add(trainCountVo_1);

        TrainCountVo trainCountVo_2 = new TrainCountVo();
        trainCountVo_2.setName("已完成");
        trainCountVo_2.setValue(finishCount);
        list.add(trainCountVo_2);

        return Result.ok(list);
    }

    @Transactional
    @Override
    public void saveTrain(Training training) {
        int createUserId = Integer.parseInt(StpUtil.getLoginId().toString());
        training.setCreateuserid(createUserId);
        this.save(training);
        List<TrainingAttachment> attachmentList = training.getAttachmentList();
        if(attachmentList != null && attachmentList.size() > 0){
            for (TrainingAttachment attachment : attachmentList) {
                attachment.setTrainingid(training.getId());
                attachment.setCreateuserid(createUserId);
            }
            trainingAttachmentService.saveBatch(attachmentList);
        }
    }
}




